"""
这个是为了将一张卷子的题目和答案进行分离，以便排版好做处理。

注意⚠️：本文件默认python运行在项目的根目录上！！！
"""
from sys import path
import os

pwd = os.path.abspath("./")

this_library_path = os.path.join(pwd, "autowork_for_office/")
path.extend([this_library_path])

# print(path)

from docx import Document
from docxes.utils import *
from docxes.editor import *

import re

pwd_up = os.path.abspath("../")
file_str = "江西2023高三开学考联考数学试题的副本"

doc = Document(pwd_up + "/" + file_str + ".docx")

paras = doc.paragraphs

RE_ANSWER_START_STRS = ["【答案】[\W\w]*", "解[:\：]"]
RE_QUESTIONS_GROUP_STR = "^[一二三四][、．\.]\w+"

# 因为试卷前面有些说明出现了与题目相同的标识，才有这样的处理
begin_para_num = 6

question_and_answer = get_blocks_paras(
    paras, RE_QUESTION_STRS, RE_ANSWER_START_STRS, start=begin_para_num
)

# print(question_and_answer)

# 获得答案的paras_nums信息。
length = len(paras)


questions=get_blocks_header(paras, RE_QUESTION_STRS, RE_ANSWER_START_STRS, start=begin_para_num
)
answers=get_blocks_tailer(paras, RE_QUESTION_STRS, RE_ANSWER_START_STRS, start=begin_para_num
)

# 生成答案的docx文件
index = 0
# 增加table的答案
table_answer = []

table = doc.add_table(2, 12)

heading_cells = table.rows[0].cells
answer_cells = table.rows[1].cells
for i in range(12):
    heading_cells[i].text = str(i + 1)
    answer_cells[i].text = table_answer[i]

move_elements(table, paras[answers[0][0]], next=False)

doc.save("答案（" + file_str + "）.docx")
del doc

# 生成试卷题目的docx文件
doc = Document(pwd_up + "/" + file_str + ".docx")
paras = doc.paragraphs

doc.save("题目（" + file_str + "）.docx")
del doc
